package com.cy.module;

import com.alibaba.fastjson.JSONObject;
import com.cy.config.EnumConst;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.Point;
import com.cy.model.PointLog;
import com.cy.model.PointRule;
import com.cy.model.User;
import com.cy.utils.TempGetPoint;
import com.jfinal.plugin.activerecord.Page;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@ControllerBind(controllerKey = "/clmp/pointRule")
public class PointRuleController extends WebApiController {
    @Override
    public void index() {
        PointRule pointRule = getModel(PointRule.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Page<PointRule> pointRulePage = PointRule.dao.paginate(page, pageSize,pointRule);
        renderBuiPage(pointRulePage, 200);
    }

    @Override
    public void show() {
        JSONObject json = new JSONObject();
        User user = getLoginUser();
        if (null == user){
            renderRetCode(EnumConst.RetCode.NOT_SIGNIN);
            return;
        }
        int interval = 0;
        int isRepeat = 0;
        try {
            isRepeat = getParaToInt("repeat");
            interval = getParaToInt("interval");
        }catch (NumberFormatException e){

        }catch (NullPointerException ne){

        }
        if(interval == 0){
        	interval = 1;
        }
        if(isRepeat == 0){
        	isRepeat = 1;
        }

        PointRule select = new PointRule();
        select.setIsRepeat(isRepeat).setRepeatInterval(interval).setStartTime(new Date()).setExpireTime(new Date()).setStatus(1);
        List<PointRule> ruleList = PointRule.dao.findPointRule(select);
        //目前 只找今日完成的任务
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);

        Calendar end = Calendar.getInstance();
        int unCompany = 0;
        for(PointRule rule : ruleList){
            PointLog sum = PointLog.dao.sumLogActionTimes(rule.getRuleId(),todayStart.getTime(),end.getTime(),user.getUserId());
            if (null != sum && null != sum.get("action_sum") && rule.getActionCount() <= sum.getLong("action_sum")){
                rule.put("isCompany",true);
            }else {
                unCompany++;
            }
        }
        //Point point = Point.dao.findByUserAndType(user.getUserId(), Point.PointType.COIN.getType());
        JSONObject dingbi = TempGetPoint.selectPoint(String.valueOf(user.getAccount()), "seach_dingbi");
        Point point = null;
        if("0".equals(dingbi.getString("ResultCode"))){
        	point = new Point();
        	point.setPointAvailable(Long.parseLong(dingbi.getString("value")));
        	point.setType(0);
        }
        
        json.put("code", EnumConst.RetCode.SUCCESS.getCode());
        json.put("ruleList", ruleList);
        json.put("unCompany", unCompany);
        json.put("point", point);
        renderJson(json);
    }

   @Override
    public void update() {
        int id = getParaToInt();
        PointRule tempPointRule = getModel(PointRule.class);
        PointRule pointRule = PointRule.dao.findById(id);
        pointRule.cover(tempPointRule);

        if (pointRule.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        PointRule pointRule = getModel(PointRule.class);
        pointRule.setCreateTime(new Date());
        if(null == pointRule.get("start_time")){
            pointRule.setStartTime(new Date());
        }
        if(null == pointRule.get("expire_time")){
            Calendar c = Calendar.getInstance();
            c.set(2065,12,30);
            pointRule.setExpireTime(c.getTime());
        }
        if (pointRule.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            PointRule.dao.deleteById(id);
        }
        renderSuccess();
    }

}
